﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence.DAO.MySQLDAO
{
    /// <summary>
    /// Data access object for user_tbl.
    /// </summary>
    internal class UserDAO : MySQLDAO, IUserDAO
    {
        #region Fields

        private MySQLDAOCmdExecuter<UserDTO> cmdExec;

        #endregion

        #region Contructor

        public UserDAO()
        {
            cmdExec = new MySQLDAOCmdExecuter<UserDTO>(this);
        }

        #endregion

        #region Methods

        private const string insertSQL =
            "INSERT INTO user_tbl (is_active, first_name, last_name, user_type) " +
            "VALUES (@is_active, @first_name, @last_name, @user_type)";
        public void Insert(UserDTO dto)
        {
            dto.ID = cmdExec.PerformInsertNonQuery(insertSQL, loadParametersFromDTO(dto));   
        }

        private const string selectByIDSQL =
            "SELECT * FROM user_tbl WHERE user_id = @user_id";
        public UserDTO SelectByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@user_id"] = id;

            MySQLDAOCmdExecuter<UserDTO>.LoadDTODelegate loadUserDTO =
                new MySQLDAOCmdExecuter<UserDTO>.LoadDTODelegate(loadDTOFromReader);
            UserDTO userDTO = cmdExec.PerformSingletonSelectQuery(selectByIDSQL, parameters, loadUserDTO);            

            return userDTO;
        }

        const string selectAllSQL = 
            "SELECT * FROM user_tbl";
        public IList<UserDTO> SelectAll()
        {
            MySQLDAOCmdExecuter<UserDTO>.LoadDTODelegate loadUserDTO =
                new MySQLDAOCmdExecuter<UserDTO>.LoadDTODelegate(loadDTOFromReader);
            return cmdExec.PerformSelectQuery(selectAllSQL, null, loadUserDTO);       
        }
      
        private const string updateSQL = 
            "UPDATE user_tbl SET is_active = @is_active, user_type = @user_type " + 
            "WHERE user_id = @user_id";
        public void Update(UserDTO userDTO)
        {
            cmdExec.PerformNonQuery(updateSQL, loadParametersFromDTO(userDTO));
        }

        const string deleteByIDSQL = 
            "DELETE FROM user_tbl WHERE user_id = @user_id";
        public void DeleteByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@user_id"] = id;
            cmdExec.PerformNonQuery(deleteByIDSQL, parameters);
        }

        private Dictionary<String, object> loadParametersFromDTO(UserDTO dto)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@user_id"] = dto.ID;
            parameters["@is_active"] = dto.IsActive;
            parameters["@first_name"] = dto.FirstName;
            parameters["@last_name"] = dto.LastName;
            parameters["@user_type"] = dto.UserType;
            return parameters;
        }

        private UserDTO loadDTOFromReader(MySqlDataReader dataReader)
        {
            UserDTO dto = new UserDTO();
            dto.ID = dataReader.GetInt64("user_id");
            dto.FirstName = dataReader.GetString("first_name");
            dto.LastName = dataReader.GetString("last_name");
            dto.IsActive = dataReader.GetBoolean("is_active");
            dto.UserType = dataReader.GetInt32("user_type");
            return dto;
        }

        #endregion
    }
}
